package top.loonglove.common.base.result;

import java.util.List;
import java.util.Objects;

/**
 * @author : xgl
 * @version : 0.0.1
 * @date :2021/8/9 1:21
 */
public class PageResult<T> {
    private int size;
    private int pageIndex;
    private List<T> content;
    private Long totalSize;
    private Long totalPage;

    public PageResult() {
    }

    public PageResult(int size, int pageIndex, List<T> content, Long totalSize, Long totalPage) {
        this.size = size;
        this.pageIndex = pageIndex;
        this.content = content;
        this.totalSize = totalSize;
        this.totalPage = totalPage;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public List<T> getContent() {
        return content;
    }

    public void setContent(List<T> content) {
        this.content = content;
    }

    public Long getTotalSize() {
        return totalSize;
    }

    public void setTotalSize(Long totalSize) {
        this.totalSize = totalSize;
    }

    public Long getTotalPage() {
        if (totalPage == null) {
            totalPage = totalSize <= size ? 1 : totalSize % size == 0 ? totalSize / size : totalSize / size + 1;
        }
        return totalPage;
    }

    public void setTotalPage(Long totalPage) {
        this.totalPage = totalPage;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof PageResult)) return false;
        PageResult<?> that = (PageResult<?>) o;
        return getSize() == that.getSize() && getPageIndex() == that.getPageIndex() && Objects.equals(getContent(), that.getContent()) && Objects.equals(getTotalSize(), that.getTotalSize()) && Objects.equals(getTotalPage(), that.getTotalPage());
    }

    @Override
    public String toString() {
        return "PageResult{" +
                "size=" + size +
                ", pageIndex=" + pageIndex +
                ", content=" + content +
                ", totalSize=" + totalSize +
                ", totalPage=" + getTotalPage() +
                '}';
    }

    @Override
    public int hashCode() {
        return Objects.hash(getSize(), getPageIndex(), getContent(), getTotalSize(), getTotalPage());
    }
}
